﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;

namespace Web.Account
{
    public partial class MyPwdChange : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = "修改密码";
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private UsersInfo GetModel(ref UsersInfo model)
        {
            string oldpwd = CommonUtils.Post("iOldPWD");
            string newpwd = CommonUtils.Post("iNewPWD");
            string newpwd2 = CommonUtils.Post("iNewPWD2");

            if (string.IsNullOrEmpty(oldpwd))
            {
                base.ResponseErch("原密码不能为空");
                return null;
            }
            if (string.IsNullOrEmpty(newpwd))
            {
                base.ResponseErch("请填写新密码");
                return null;
            }
            if (string.IsNullOrEmpty(newpwd2))
            {
                base.ResponseErch("请确认新密码");
                return null;
            }
            //pwd = CommonUtils.MD5(pwd);

            model.PassWord = CommonUtils.MD5(newpwd2);
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                UsersInfo user = this.userInfo;
                user = GetModel(ref user);

                rtmessage = ServiceFactory.CreateInstance<UsersService>().UpdatePWD(user);
                resultMsg = "用户信息修改成功!";
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}
